/****
 * add trim(),wbr() and replaceAll() method to String
 */
String.prototype.trim=function(){
    return this.replace(/^\s+|\s+$/g,'');
};
String.prototype.replaceAll=function(str,rep){
    return this.split(str).join(rep);
};
String.prototype.wbr=function(len){
    if(!len)len=10;
    if(this.length<len)return this;
    var cnt=0;
    var rt="",txt=this;
    for(var i=0;i<txt.length; i++){
        if(cnt>=len){
            cnt=0;
            rt +="<wbr>";
        }
        var c=txt.charAt(i);
        rt +=c;
        if(c>='0' && c<='9')cnt ++;
        if(c>='a' && c<='z')cnt ++;
        if(c>='A' && c<='Z')cnt ++;
    }
    return rt;
};
/**
 * add containsObject() to Array objects
 */
Array.prototype.containsObject=function(p){
    for (var i=0;i<this.length;i++){
        if(p==this[i])return true;
    }
    return false;
}
/**
 * abbreviated usages
 */
var $d=document;

/*****************************
 * shared methods for console webs
 *****************************/
var $f={
    //apppend a class to node
    addClass:function(node,clazz){
        var c=" "+node.className+" ";
        if(!c){
            c=clazz;
        }else if(c.indexOf(" "+clazz+" ") < 0){
            c=c.trim()+" "+clazz;
        }
        node.className=c;
    },
    //remove a class from node
    delClass:function(node,clazz){
        if(!clazz || clazz=="")return;
        var c = (" "+node.className+" ");
        c=c.replace(" " + clazz + " "," ");
        c=c.trim();
        node.className=c;
    },
    // get all properties of object
    ps:function(obj){
        var ps="";
        var all=new Array();
        for(var p in obj){
        	all.push(p);
            //ps+=p+"\t";
        }
        all.sort();
        alert(all.join("\t"));
    },
    //getSelect 取得checkbox选中的value属性值
	getSelected:function(checkboxes){
	    var ret =new Array();
	    if(checkboxes==null){
	        return ret;
	    }
	    for(var i=0,j=0;i<checkboxes.length;i++){
	        if(chs[i].checked){
	            ret[j ++]=chs[i].value;
	        }
	    }
	    return ret;
	},
	getParentNode:function(node,tag){
        do{
            if(node.tagName)if(node.tagName.toUpperCase()==tag){
                return node;
            }
            node =node.parentNode;
        }while(node);
        return false;
    }
};

/**************************
 * table methods
 ***************************/
$rows={
    //change class of rows
    setClass:function(table){
        var tb=table.getElementsByTagName("TBODY");
        if(!tb || tb.length<1)return;
        var rs=tb[0].getElementsByTagName("TR");
        if(!rs || rs.length<1)return;
        for(var i=0;i<rs.length;i++){
            var r=rs[i];
            var t=(i%2);
            $f.delClass(r, "row"+(1-t));
            $f.addClass(r, "row"+t);
        }
    },
    //find whether there be on selected checkbox
    checkHasSelectedBoxes:function(ele,name){
        var table=$rows.getParentTable(ele);
        if(!table)return false;
        var chks=$rows.getFirstCheckboxes(table,name);
        if(!chks)return false;
        if(chks.length<1)return false;
        if(chks.length==1&&chks[0]==ele)return false;
        for(var i=0;i<chks.length-1;i++){
            if(!chks[i].disabled)return true;
        }
        return false;
    },
    //get all selected checkboxs
    getSelectedCheckboxs:function(table,name){
        var chks=this.getFirstCheckboxes(table,name);
        var arr=new Array();
        for(var i=0;i<chks.length-1;i++){
            if(!chks[i].checked)continue;
            arr[arr.length]=chks[i];
        }
        return arr;
    },
    // get all checkboxes(selected or not)
    getCheckboxSelects:function(table,name){
        var chks=this.getFirstCheckboxes(table,name);
        return chks.slice(0,chks.length-1);
    },
    // get checkbox in the first cell of head rows
    getCheckboxAllSelect:function(table,name){
        var chks=this.getFirstCheckboxes(table,name);
        return chks[chks.length-1];
    },
    //get table checkbox selection status,with has checked and all checked status
    getCheckboxStatus:function(table,name,checkbox){
        var chks=this.getFirstCheckboxes(table,name);
        var allCheck=chks[chks.length-1];
        allCheck.checked=checkbox?checkbox.checked:true;
        var hasChecked=false;
        for(var i=chks.length-2;i>=0;i--){
            if(chks[i].disabled)continue;
            if(chks[i].checked){
                hasChecked=true;
            }else{
                allCheck.checked=false;
            }
        }
        return {
            allChecked:allCheck.checked,
            hasChecked:hasChecked
        };
    },
    //
    selectCheckboxAll:function(checkbox,name){
        var table=$rows.getParentTable(checkbox);
        var chks=this.getFirstCheckboxes(table,name);
        for(var i=chks.length-2;i>=0;i--){
            if(chks[i].disabled)continue;
            this.selectCheckbox(chks[i],checkbox.checked);
        }
    },
    selectCheckbox:function(chk,checked){
        var c = checked;
        if(null==checked)c=chk.checked;
        var row = $f.getParentNode(chk,"TR");
        if(c){
            $f.addClass(row,"row2");
        }else{
            $f.delClass(row,"row2");
        }
        row._selected_=c;
        if(null!=checked)chk.checked=checked;
    },
    onSelectCheckbox:function(checkbox){
        this.selectCheckbox(checkbox);
        var table=$rows.getParentTable(checkbox);
        return this.getCheckboxStatus(table, checkbox.name, checkbox);
    },
    
    getParentTable:function(node){
        return $f.getParentNode(node, "TABLE");
    },
    getFirstCheckboxes:function(table,name){
        var rs=table.rows;
        var chks=new Array();
        var chkAll=false;
        for(var i=0;i<rs.length;i++){
            var cs=rs[i].cells;
            for(var j=0;j<2&&j<cs.length;j++){
                var ns=cs[j].childNodes;
                for(var k=0;k<ns.length;k++){
                    var n=ns[k];
                    if(n.type!="checkbox")continue;
                    if(n.name!=name){
                        if(!chkAll && i==0 && (!n.name || n.name=="")){
                            chkAll=n;
                        }
                        continue;
                    }
                    chks.push(n);
                    ns=false;
                    break;
                }
                if(!ns)break;
            }
        }
        chks.push(chkAll);
        return chks;
    },
    currow:null,
    overRow:function(row){
        if(this.currow)this.outRow(this.currow);
        this.currow=row;
        $f.addClass(row, "CurrentRow");
    },
    outRow:function(row){
        $f.delClass(row, "CurrentRow");
        this.currow=null;
    }
}



